import React, { memo, useEffect, useState } from "react";
import { WebView } from '@tarojs/components'
import { navigateBack, useRouter } from '@tarojs/taro';

import './index.scss'


function OtherView() {
  const router = useRouter()
  const [src, setSrc] = useState('')
  useEffect(() => {
    router.params?.src && setSrc(decodeURIComponent(router.params.src))
  }, [router.params])


  const handleWebViewMessage = (e) => {
    console.log(e)
    const { data ,src} = e.detail;
    // 在这里处理 WebView 发送的消息
    console.log('Received message from WebView:', data);
   
    // 在这里拦截 WebView 加载的 URL  .contains('pds://esign/callback_oauth')  contains('pds://esign/callback_signflow')
    if (src.indexOf('pds://esign/callback_oauth')) {
      // 执行特殊处理
      console.log('Special page loaded in WebView!');
      Taro.navigateBack({delta:1});
    }
  }

  const onLoad = (e) => {
    console.log(e)
  }

  return (
    <WebView src={src} onMessage={handleWebViewMessage} onLoad={onLoad} />
  )

}

export default memo(OtherView)
